package com.glhtgyxy.biyesheji.Mapper;

import com.glhtgyxy.biyesheji.Pojo.house;
import com.glhtgyxy.biyesheji.Pojo.merchant;
import org.apache.ibatis.annotations.*;

import java.util.ArrayList;
import java.util.HashMap;

@Mapper
public interface houseMapper {

    @Select("select * from house")
    ArrayList<house> findAllHouse();

    @Select("select * from house where masterid = #{id}")
    ArrayList<house> findmerchantHouse(int id);

    @Select("SELECT * FROM house WHERE id LIKE CONCAT('%', #{searchtext}, '%') OR housename LIKE CONCAT('%', #{searchtext}, '%') OR addr LIKE CONCAT('%', #{searchtext}, '%') OR price LIKE CONCAT('%', #{searchtext}, '%') OR bedroom LIKE CONCAT('%', #{searchtext}, '%') OR facilityservices LIKE CONCAT('%', #{searchtext}, '%') OR masterid LIKE CONCAT('%', #{searchtext}, '%')")
    ArrayList<house> findHouse(@Param("searchtext") String searchtext);

    @Delete("delete from house where id = #{id}")
    int deleteHouse(@Param("id") int id);


    @SelectProvider(type = houseMapper.HouseSqlProvider.class, method = "findHouse")
    ArrayList<house> findmerchanthouseandtiaojian(HashMap<String, String> map);

    public class HouseSqlProvider{
        public String findHouse(HashMap<String, String> map) {
            StringBuilder sql = new StringBuilder("SELECT * FROM house WHERE masterid = #{id} ");

            if (map.containsKey("addr") && map.get("addr") != null) {
                sql.append("AND addr like concat('%',#{addr},'%')");
            }

            if (map.containsKey("pricelow") && map.containsKey("priceheigh")) {
                sql.append("AND price BETWEEN #{pricelow} AND #{priceheigh} ");
            } else if (map.containsKey("pricelow")) {
                sql.append("AND price >= #{pricelow} ");
            } else if (map.containsKey("priceheigh")) {
                sql.append("AND price <= #{priceheigh} ");
            }

            if (map.containsKey("bedroom") && map.get("bedroom") != null) {
                sql.append("AND bedroom like concat('%',#{bedroom},'%') ");
            }
            if (map.containsKey("search") && map.get("search") != null) {
                sql.append("AND (addr like concat('%',#{search},'%') or price like concat('%',#{search},'%') or bedroom like concat('%',#{search},'%') or housename like concat('%',#{search},'%') or id like concat('%',#{search},'%') or facilityservices like concat('%',#{search},'%') ) ");
            }

            return sql.toString();
        }
    }


}
